import {
    getToken,
    setToken,
    removeToken,
} from '../../config/authConfig'
import { reqLogin, reqLogout,  reqUserInfo } from '../../api/clientApi'

import {
    getMemberInfo,
    getUserInfo,
    removeMemberInfo,
    removeUserInfo,
    setMemberInfo,
    setUserInfo
} from '../../utils/storageUtil'

const user = {
    namespaced: true,
    state: {
        isLogin: !!getToken(),
        token: getToken(),
        userInfo: getUserInfo(),
        memberInfo: getMemberInfo(),
        administrator: getUserInfo().type == 1||getUserInfo().type == 2,
    },
    mutations: {
        SET_TOKEN: (state, token) => {
            state.token = token
            state.isLogin=!!token
        },
        SET_INFO: (state, info) => {
            state.userInfo = info
            state.administrator=info.type==1||info.type==2
        },
        SET_MEMBER_INFO: (state, info) => {
            state.memberInfo = info
        },

    },
    actions: {
        /**
         * 登录
         * @param commit
         * @param dispatch
         * @param data
         * @returns {Promise<unknown>}
         * @constructor
         */
        login({ commit, dispatch }, data) {
            return new Promise((resolve, reject) => {
                dispatch('getLogin', data).then(() => {
                    dispatch('getUserInfo').then((data) => {
                        resolve(data)
                    }).catch(error => {
                        reject(error)
                    })
                }).catch(res => {
                    reject(res)
                })
            })
        },
        /**
         * 登录
         * @param commit
         * @param dispatch
         * @param data
         * @returns {*}
         * @constructor
         */
        loginWithToken({ commit, dispatch }, data) {
            // const tokenStr = data.tokenHead + data.token
            const tokenStr = data.token
            setToken(tokenStr)
            commit('SET_TOKEN', tokenStr)
            return dispatch('getUserInfo')
        },
        loginWithUserInfo({ commit, dispatch }, data) {
            // const tokenStr = data.tokenHead + data.token
            const tokenStr = data.token
            setToken(tokenStr)
            commit('SET_TOKEN', tokenStr)
            setUserInfo(data.user)
            commit('SET_INFO', data.user)
        },
        /**
         * 登录
         * @param commit
         * @param dispatch
         * @param data
         * @returns {Promise<unknown>}
         * @constructor
         */
        loginNeedMember({ commit, dispatch }, data) {
            return new Promise((resolve, reject) => {
                dispatch('getLogin', data).then(() => {
                    Promise.all([dispatch('getUserInfo'), dispatch('getMemberInfo')])
                           .then(() => {
                               resolve()
                           }).catch(res => {
                        reject(res)
                    })
                }).catch(res => {
                    reject(res)
                })
            })
        },
        /**
         * 获取登录
         * @param commit
         * @param dispatch
         * @param params
         * @returns {Promise<unknown>}
         * @constructor
         */
        getLogin({ commit, dispatch }, params) {
            return new Promise((resolve, reject) => {
                reqLogin(params).then(data => {
                    // const tokenStr = data.tokenHead + data.token
                    const tokenStr =  data.token
                    setToken(tokenStr)
                    commit('SET_TOKEN', tokenStr)
                    resolve(data)
                }).catch(res => {
                    reject(res)
                })
            })
        },
        /**
         * 获取用户信息
         * @param commit
         * @returns {Promise<unknown>}
         * @constructor
         */
        getUserInfo({ commit }) {
            return new Promise((resolve, reject) => {
                reqUserInfo().then(data => {
                    setUserInfo(data)
                    commit('SET_INFO', data)
                    resolve(data)
                }).catch(res => {
                    reject(res)
                })
            })
        },
        /**
         * 获取用户权限信息
         * @param commit
         * @constructor
         */
        getMemberInfo({ commit }) {
            return new Promise((resolve, reject) => {
                reqMemberInfo().then(data => {
                    //data.menuList = recursionGenerateTreeBySort(data.menuList)
                    data.resList = data.resourceList.map(item => item.frontUrl).filter(it => it)
                    console.log(data.menuList)
                    setMemberInfo(data)
                    commit('SET_MEMBER_INFO', data)
                    resolve(data)
                }).catch(res => {
                    reject(res)
                })
            })
        },
        /**
         * 后端登出
         * @param commit
         * @returns {Promise<unknown>}
         * @constructor
         */
        logout({ commit }) {
            return new Promise((resolve) => {
                reqLogout().then(() => {
                    commit('SET_TOKEN', '')
                    removeToken()
                    removeUserInfo()
                    removeMemberInfo()
                    resolve()
                }).catch(() => {
                    commit('SET_TOKEN', '')
                    removeToken()
                    removeUserInfo()
                    removeMemberInfo()
                    resolve()
                })
            })
        },
        /**
         * 前端登出
         * @param commit
         * @returns {Promise<void>}
         * @constructor
         */
        fedLogout({ commit }) {
            commit('SET_TOKEN', '')
            commit('SET_INFO', {})
            removeToken()
            removeUserInfo()
            removeMemberInfo()
            return Promise.resolve()
        }
    }
}

export default user
